package com.mascotikas.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.Widget;
import com.mascotikas.client.events.GalleryUpdatedEvent;
import com.mascotikas.client.events.GalleryUpdatedEventHandler;
import com.mascotikas.client.jdos.LoginInfo;
import com.mascotikas.client.services.IUserImageService;
import com.mascotikas.client.services.IUserImageServiceAsync;
import com.mascotikas.shared.UploadedImage;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.Window;

import com.smartgwt.client.widgets.viewer.DetailViewer;
import com.smartgwt.client.widgets.viewer.DetailViewerField;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;

public class UploadPhoto extends Composite implements HasHandlers {

    private static UploadPhotoUiBinder uiBinder = GWT.create(UploadPhotoUiBinder.class);
    IUserImageServiceAsync userImageService = GWT.create(IUserImageService.class);
    private HandlerManager handlerManager;

    interface UploadPhotoUiBinder extends UiBinder<Widget, UploadPhoto> {
    }
    @UiField
    Button uploadButton;
    @UiField
    FormPanel uploadForm;
    @UiField
    FileUpload uploadField;
    LoginInfo loginInfo;

    public UploadPhoto(final LoginInfo loginInfo) {
        handlerManager = new HandlerManager(this);

        this.loginInfo = loginInfo;

        initWidget(uiBinder.createAndBindUi(this));

        uploadButton.setText("Upload");
        uploadButton.setText("Loading...");
        uploadButton.setEnabled(false);

        uploadField.setName("image");

        startNewBlobstoreSession();

        uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

            @Override
            public void onSubmitComplete(SubmitCompleteEvent event) {
                uploadForm.reset();
                startNewBlobstoreSession();

                String key = event.getResults();

                userImageService.get(key, new AsyncCallback<UploadedImage>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onSuccess(UploadedImage result) {

                        final DetailViewer detailViewer = new DetailViewer();
                        DetailViewerField imgDetalleField = new DetailViewerField("picture");
                        detailViewer.setWidth(500);
                        detailViewer.setTop(400);
                        detailViewer.setEmptyMessage("Click a row in the grid");
                        Record record = new Record();
                        record.setAttribute("picture", result.getServingUrl());
                        record.setAttribute("raza", "others");
                        record.setAttribute("ubicacion", result.getKey());
                        detailViewer.setData(new Record[]{record});
                        imgDetalleField.setType("image");
                        imgDetalleField.setImageWidth(400);
                        imgDetalleField.setImageHeight(300);
                        detailViewer.setFields(imgDetalleField);
                        final Window winModal = new Window();
                        winModal.setAutoSize(Boolean.TRUE);

                        winModal.setTitle("Detalle");
                        winModal.setShowMinimizeButton(false);
                        winModal.setCanDragReposition(Boolean.FALSE);
                        winModal.setIsModal(true);
                        winModal.setShowModalMask(true);
                        winModal.setAutoCenter(Boolean.TRUE);
                        winModal.addCloseClickHandler(new CloseClickHandler() {

                            public void onCloseClick(CloseClientEvent event) {
                                setTitle("cerrar");
                                winModal.destroy();
                            }
                        });
                        FormDataImage form = new FormDataImage(winModal);
                        form.setPhotoKey(result.getKey());
                        winModal.addItem(detailViewer);
                        winModal.addItem(form);
                        winModal.addItem(form.getRichTextEditor());
                        winModal.addItem(form.getValidateItem());
                        winModal.removeFromParent();
                        winModal.show();

//                        ImageOverlay overlay = new ImageOverlay(
//                                result, loginInfo);
                        GalleryUpdatedEvent event = new GalleryUpdatedEvent();
                        fireEvent(event);
//
//                        // TODO: Add something here that says,
//                        // hey, upload succeeded
//
//                        final PopupPanel imagePopup = new PopupPanel(
//                                true);
//                        imagePopup.setAnimationEnabled(true);
//                        // imagePopup.setWidget(overlay);
//                        imagePopup.setGlassEnabled(true);
//                        imagePopup.setAutoHideEnabled(true);
//
//                        imagePopup.center();

                    }
                });

            }
        });
    }

    private void startNewBlobstoreSession() {
        userImageService.getBlobstoreUploadUrl(new AsyncCallback<String>() {

            @Override
            public void onSuccess(String result) {
                uploadForm.setAction(result);
                uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
                uploadForm.setMethod(FormPanel.METHOD_POST);

                uploadButton.setText("Upload");
                uploadButton.setEnabled(true);

            }

            @Override
            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
            }
        });
    }

    @UiHandler("uploadButton")
    void onSubmit(ClickEvent e) {
        uploadForm.submit();
    }

    @Override
    public void fireEvent(GwtEvent<?> event) {
        handlerManager.fireEvent(event);
    }

    public HandlerRegistration addGalleryUpdatedEventHandler(
            GalleryUpdatedEventHandler handler) {
        return handlerManager.addHandler(GalleryUpdatedEvent.TYPE, handler);
    }
}
